<template>
  <div class="monitor-rule">
    <el-card>
      <tag-content-header add search refresh setting @toolFun="toolFun" :settingData="columns">
        <template #title>
          <span>标签分布区间规则预警列表</span>
        </template>
      </tag-content-header>
      <tag-table
        ref="table"
        @refresh="getList"
        :data="dataList"
        :loading="loading"
        :columns="columns"
        :params="params"
        :total="totalSize"
      >
        <template #operation="{ scope }">
          <div class="operation-button-box">
            <el-tooltip effect="dark" content="编辑" placement="top" :enterable="false">
              <icon-svg name="iconEdit" class="svg-edit" @click.native="handleEdit(scope.row)"></icon-svg>
            </el-tooltip>
            <el-tooltip effect="dark" content="删除" placement="top" :enterable="false">
              <icon-svg name="iconDelete" class="svg-delete" @click.native="handleDelete(scope.row)"></icon-svg>
            </el-tooltip>
          </div>
        </template>
      </tag-table>
    </el-card>
    <rule-drawer ref="ruleDrawer" @refresh="getList"></rule-drawer>
  </div>
</template>

<script>
import ruleDrawer from './monitor-rule-drawer'
export default {
  components: {
    ruleDrawer
  },
  data() {
    return {
      loading: false,
      dataList: [],
      columns: [
        {
          show: true,
          label: '标签名称',
          prop: 'tagNm',
          search: true,
          searchKey: ''
        },
        {
          show: true,
          label: '区间/枚举值个数',
          prop: 'codeNum',
          align: 'center'
        },
        {
          show: true,
          label: '创建时间',
          prop: 'createTm',
          align: 'center'
        },
        {
          show: 'fixed',
          label: '操作',
          prop: 'operation',
          align: 'center',
          width: 200
        }
      ],
      params: {
        ascs: [],
        descs: [],
        currentPageNum: 1,
        pageSize: 10,
        queryParams: {},
        descPriority: false
      },
      totalSize: 0
    }
  },
  methods: {
    getDetail() {
      this.getList()
    },
    getList() {
      this.loading = true
      this.$ajax({
        url: this.$ajax.setUrl('tag/tagDataBox/selectTagBoxConfigPage'),
        method: 'get',
        params: this.$ajax.setParams(this.params)
      }).then(({ data }) => {
        if (data && data.code === '0') {
          if (data.data.currentPageNum === this.params.currentPageNum) {
            this.dataList = data.data.records
            this.totalSize = data.data.totalSize
          }
        } else {
          this.$message.error(data.data || data.msg)
        }
        this.loading = false
      })
    },
    toolFun(operation) {
      switch (operation) {
        case 'add':
          this.$refs.ruleDrawer.showDrawer({ type: 'add' })
          break
        case 'search':
          // 搜索动作
          this.$refs['table'].searchToggle()
          break
        case 'refresh':
          // 刷新动作
          this.getList()
          break
      }
    },
    handleDelete(row) {
      this.$confirm('确认删除【' + row.tagNm + '】？')
        .then(() => {
          let messageBox = document.getElementsByClassName('el-message-box__wrapper')
          messageBox[0].focus() // 防止按空格键重复确定
          this.$ajax({
            url: this.$ajax.setUrl(`tag/tagDataBox/deleteTagBoxConfig/${row.tagId}`),
            method: 'delete',
            params: this.$ajax.setParams()
          }).then(({ data }) => {
            if (data && data.code === '0') {
              this.getList()
              this.$message.success('操作成功')
            } else {
              this.$message.error(data.data || data.msg)
            }
          })
        })
        .catch(() => {})
    },
    handleEdit(row) {
      this.$refs.ruleDrawer.showDrawer({ type: 'edit', tagId: row.tagId })
    }
  }
}
</script>

<style lang="scss"></style>
